package com.arvato.minipro.service.mall;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.arvato.minipro.bean.bean_do.mall.EcAddressDo;
import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_vo.mall.EcAddressVo;
import com.arvato.minipro.mapper.mall.EcAddressMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * Describe：用户收货地址的service
 * 
 * @author smallking
 *
 *  2018年2月8日
 */
@Service
@Transactional
public class AddressService {
	
	@Autowired
	private EcAddressMapper ecAddressMapper;
	
	
	/**
	 * 根据Query条件查询多条收货地址列表
	 * @param query
	 * @return
	 */
	public PageData listAddressByQuery(Query query) {
		// 开启分页 设置参数
		PageHelper.startPage(query.getPageNum(), query.getPageSize());
		// 开始查询
		List<EcAddressVo> list = ecAddressMapper.listAddressByQuery(query);
		// 得到分页结果
		PageInfo<EcAddressVo> pageInfo = new PageInfo<EcAddressVo>(list);
		
		return new PageData(pageInfo.getTotal(), pageInfo.getList());
	}
	
	/**
	 * 新增收货地址
	 * @param addressDo
	 * @return
	 */
	public int insert(EcAddressDo addressDo) {
		int row = ecAddressMapper.insert(addressDo);
		if(row == 1 && "1".equals(addressDo.getDefaultAddress() + "")) {
			// 将该用户其他的地址给设置成非默认
			ecAddressMapper.updateDefaultAddress(addressDo.getAddressId());
		}
		return row;
	}

	public int update(EcAddressDo addressDo) {
		int row = ecAddressMapper.update(addressDo);
		if(row == 1 && addressDo.getDefaultAddress() == 1) {
			// 将该用户其他的地址给设置成非默认
			ecAddressMapper.updateDefaultAddress(addressDo.getAddressId());
		}
		return row;
	}
	

}
